package com.burton.dp;

import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.retry.ExponentialBackoffRetry;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.data.Stat;

public class a {
	 private static final Logger log =LogManager.getLogger(a.class);

	
	
	private static String ZOOKEEPER_SERVER_STRING = "127.0.0.1:2181";

	static {
		// ZOOKEEPER_SERVER_STRING =
		// ServiceConfig.get("zookeeper.serverString");
		// log.info("ZOOKEEPER_SERVER_STRING=" + ZOOKEEPER_SERVER_STRING);
	}

	public static void main(String[] args) throws Exception {
		System.out.println(ZOOKEEPER_SERVER_STRING);
		CuratorFramework client = CuratorFrameworkFactory
				.builder()
				.connectString(ZOOKEEPER_SERVER_STRING)
				.sessionTimeoutMs(5000)
				.namespace("base")
				.retryPolicy(
						new ExponentialBackoffRetry(5000, Integer.MAX_VALUE))
				.build();
		client.start();
		String path ="/burton";
		createZnode(client, "/burton", "hello");
//		update(client, path);
//		delete(client, path);
//		checkExist(client, path);
		getData(client, path);
	}

	public static void createZnode(CuratorFramework client, String path,
			String content) throws Exception {
		log.info("create znode: path=" + path + ",content=" + content);
		client.create().creatingParentsIfNeeded()
				.withMode(CreateMode.EPHEMERAL)
				.forPath(path, content.getBytes());
	}
	
	
	
	public static void update(CuratorFramework client,String path){
		try {
			Stat stat= new Stat();
			client.getData().storingStatIn(stat).forPath(path);
			int version= client.setData().withVersion(stat.getVersion()).forPath(path, "burton@wang".getBytes()).getVersion();
		    System.out.println("suc update path:"+path+";version="+version);
		    client.setData().withVersion(stat.getVersion()).forPath(path, "burton@min".getBytes()).getVersion();
		} catch (Exception e) {
			// TODO Auto-generated catch block
//			e.printStackTrace();
			System.out.println("fail update path:"+path+";version=");
		}
	}
	
	public static void getData(CuratorFramework client,String path){
		try {
			System.out.println("getData===>>"+new String(client.getData().forPath(path)));
		} catch (Exception e) {
			// TODO Auto-generated catch block
//			e.printStackTrace();
			System.out.println("fail update path:"+path+";version=");
		}
	}
	
	public static void delete(CuratorFramework client,String path){
		try {
//			Stat stat= new Stat();
//			client.getData().storingStatIn(stat).forPath(path);
			client.delete().deletingChildrenIfNeeded().forPath(path);
		} catch (Exception e) {
			// TODO Auto-generated catch block
//			e.printStackTrace();
			System.out.println("fail update path:"+path+";version=");
		}
	}

	
	
	 public static void checkExist(CuratorFramework client,String path) throws Exception{
		 
		if(client.checkExists().forPath(path) == null){
			System.out.println("no");
		}else{
			System.out.println("exists");
		}
	 }
	
}